/*
 * Copyright (C) 2012 rothens
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package rpg.ui.components;

import rpg.gfx.Bitmap;
import rpg.util.ImageUtil;

/**
 *
 * @author rothens
 */
public class ComponentCheckBox extends Component implements IClickableComponent {

    private boolean checked = false;
    private Bitmap background_inactive;
    private Bitmap background_active;
    private Bitmap background_hover;
    private Bitmap tick = ImageUtil.loadBitmap("/res/backgrounds/tick.png");

    public ComponentCheckBox(Integer x, Integer y) {
        super(30, 30, x, y);
        background_active = new Bitmap(width, height);
        background_active.clearWith(0xaa000001);
        background_active = StaticTextures.border_active.overlay(background_active);

        background_inactive = new Bitmap(width, height);
        background_inactive.clearWith(0xaa000001);
        background_inactive = StaticTextures.border_inactive.overlay(background_inactive);

        background_hover = new Bitmap(width, height);
        background_hover.clearWith(0xaa000001);
        background_hover = StaticTextures.border_hover.overlay(background_hover);

    }

    public ComponentCheckBox(Integer x, Integer y, boolean checked) {
        this(x, y);
        this.checked = checked;
    }

    @Override
    public void redraw() {
        clear();

        if (state == ComponentState.HOVER) {
            draw(background_hover, 0, 0);
        } else if (state == ComponentState.SELECTED) {
            draw(background_active, 0, 0);
        } else {
            draw(background_inactive, 0, 0);
        }
        if (checked) {
            draw(tick, 0, 0);
        }
        if (parent != null) {
            ((Component) parent).redraw();
        }
    }
    
    public void setChecked(boolean checked){
        this.checked = checked;
        redraw();
    }
    
    public boolean isChecked(){
        return checked;
    }

    @Override
    public boolean onPress(int x, int y) {
        return false;
    }

    @Override
    public boolean onRelease(int x, int y) {
        if (isMouseOver(x, y)) {
            checked = !checked;
            redraw();
        }
        return false;
    }
}
